from app.account.models import UserLeaderVerifyForm
from common.authCheck.adminAuth import check_admin_login
from common.dictInfo import model_to_dict
from django.http import JsonResponse
from rest_framework.views import APIView
from .chiefAdminAuthVerify import chiefAdminCheck


class AdminVerifyUserLeaderView(APIView):

    @check_admin_login
    @chiefAdminCheck
    def get(self, request, fid):
        """
        获取申请表单详情
        :param request:
        :param fid:
        :return:
        """
        verifyForm = UserLeaderVerifyForm.objects.filter(id=fid)
        if not verifyForm.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '表单不存在'
            }, status=404)
        verifyForm = verifyForm[0]
        verifyFormDict = model_to_dict(verifyForm, exclude='reject_information')
        return JsonResponse({
            'status': True,
            'form': verifyFormDict
        })

    @check_admin_login
    @chiefAdminCheck
    def put(self, request, fid):
        """
        表单通过审核认证
        :param request:
        :param fid:
        :return:
        """
        verifyForm = UserLeaderVerifyForm.objects.filter(id=fid)
        if not verifyForm.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '表单不存在'
            }, status=404)
        verifyForm = verifyForm[0]
        verifyForm.status = '2'
        verifyForm.save()
        verifyForm.related_user.user_role = '99'
        verifyForm.related_user.save()
        return JsonResponse({
            'status': True,
            'id': verifyForm.id,
            'user_id': verifyForm.related_user.id
        })

    @check_admin_login
    @chiefAdminCheck
    def delete(self, request, fid):
        """
        拒绝表单
        :param request:
        :param fid:
        :return:
        """
        verifyForm = UserLeaderVerifyForm.objects.filter(id=fid)
        if not verifyForm.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '表单不存在'
            }, status=404)
        verifyForm = verifyForm[0]
        import json
        jsonParams = json.loads(request.body.decode('uft-8'))
        verifyForm.reject_information = jsonParams.get('reject_information')
        verifyForm.status = '3'
        verifyForm.save()
        return JsonResponse({
            'status': True,
            'id': verifyForm.id,
            'user_id': verifyForm.related_user.id
        })
